<?php
/**
 * undocumented 
 *
 * @package Genesis
 */

/**
 * Display Breadcrumbs above the Loop
 * Concedes priority to popular breadcrumb plugins
 *
 * @since 0.1.6
 */
add_action('genesis_before_loop', 'genesis_do_breadcrumbs');
function genesis_do_breadcrumbs() {
	
	// Conditional Checks
	if( is_front_page() && !genesis_get_option( 'breadcrumb_home' ) ) return;
	if( is_single() && !genesis_get_option( 'breadcrumb_single' ) ) return;
	if( is_page() && !genesis_get_option( 'breadcrumb_page' ) ) return;
	if( ( is_archive() || is_search() ) && !genesis_get_option('breadcrumb_archive')) return;

	if ( function_exists( 'bcn_display' ) ) {
		echo '<div class="breadcrumb">'; bcn_display(); echo '</div>';
	}
	elseif ( function_exists( 'yoast_breadcrumb' ) ) {
		yoast_breadcrumb('<div class="breadcrumb">','</div>');
	}
	elseif ( function_exists( 'breadcrumbs' ) ) {
		breadcrumbs();
	}
	elseif ( function_exists( 'crumbs' ) ) {
		crumbs();
	}
	else {
		genesis_breadcrumb();
	}
}

/**
 * This function displays a breadcrumb navigation trail.
 *
 * @author Joost de Valk
 * @since 0.1
 */
function genesis_breadcrumb($args = array()) {
	global $wp_query, $post;
	
	$defaults = array(
		'home' => __( 'Home', 'genesis' ),
		'sep' => '/',
		'prefix' => '<div class="breadcrumb">',
		'suffix' => '</div>',
		'labels' => array(
				'prefix' => __('You are here:', 'genesis'),
				'author' => __('Archives for','genesis'),
				'tag' => __('Archives for','genesis'),
				'date' => __('Archives for','genesis'),
				'search' => __('Search for','genesis')
			),
		'display' => TRUE
	);
	$args = wp_parse_args($args, $defaults);
	$args = apply_filters('genesis_breadcrumb_args', $args);
	
	function bold_or_not( $input ) {
		// return '<strong>'.$input.'</strong>';
		return $input;
	}
	// Copied and adapted from WP source
	function sp_get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE){
		$chain = '';
		$parent = &get_category($id);
		if ( is_wp_error( $parent ) )
		   return $parent;

		if ( $nicename )
		   $name = $parent->slug;
		else
		   $name = $parent->cat_name;

		if ( $parent->parent && ($parent->parent != $parent->term_id) )
		   $chain .= get_category_parents($parent->parent, true, $separator, $nicename);

		$chain .= bold_or_not($name);
		return $chain;
	}
	
	$on_front = get_option('show_on_front');
	
	if ($on_front == "page") {
		$homelink = '<a href="'.get_permalink(get_option('page_on_front')).'">'.$args['home'].'</a>';
		$bloglink = get_option('page_for_posts');
		if( $bloglink ) {
			$bloglink = $homelink.' '.$args['sep'].' <a href="'.get_permalink(get_option('page_for_posts')).'">'.get_the_title(get_option('page_for_posts')).'</a>';
		} else {
			$bloglink = $homelink;
		}
	} else {
		$homelink = '<a href="'.get_bloginfo('url').'">'.$args['home'].'</a>';
		$bloglink = $homelink;
	}
	
	$homelink = apply_filters('genesis_breadcrumb_homelink', $homelink);
	$bloglink = apply_filters('genesis_breadcrumb_bloglink', $bloglink);
		
	if ( ($on_front == "page" && is_front_page()) || ($on_front == "posts" && is_home()) ) {
		$output = $args['home'];
	} elseif ( $on_front == "page" && is_home() ) {
		$output = $homelink.' '.$args['sep'].' '.get_the_title(get_option('page_for_posts'));
	} elseif ( !is_page() ) {
		$output = $bloglink.' '.$args['sep'].' ';
		if ( is_single() ) {
			$cats = get_the_category();
			$cat = $cats[0];
				
			if ( $cat->parent != 0 ) {
				$output .= get_category_parents($cat->term_id, true, " ".$args['sep']." ");
			} elseif ( !is_wp_error(get_category_link($cat->term_id)) ) { 
				$output .= '<a href="'.get_category_link($cat->term_id).'">'.esc_html($cat->name).'</a> '.$args['sep'].' '; 
			}
		}
		if ( is_category() ) {
			$cat = intval( get_query_var('cat') );
			$output .= sp_get_category_parents($cat, false, " ".$args['sep']." ");
		} elseif ( is_tag() ) {
			$output .= bold_or_not($args['labels']['tag']." ".single_cat_title('',false));
		} elseif (is_date()) { 
			$output .= bold_or_not($args['labels']['date']." ".single_month_title(' ',false));
		} elseif (is_author()) { 
			$user = get_userdatabylogin($wp_query->query_vars['author_name']);
			$output .= bold_or_not($args['labels']['author']." ".esc_html($user->display_name));
		} elseif (is_search()) {
			$output .= bold_or_not($args['labels']['search'].' "'.esc_html(apply_filters('the_search_query', get_search_query())).'"');
		} else {
			$output .= bold_or_not(get_the_title());
		}
	} else {
		$post = $wp_query->get_queried_object();

		// If this is a top level Page, it's simple to output the breadcrumb
		if ( 0 == $post->post_parent ) {
			$output = $homelink." ".$args['sep']." ".bold_or_not(get_the_title());
		} else {
			if (isset($post->ancestors)) {
				if (is_array($post->ancestors))
					$ancestors = array_values($post->ancestors);
				else 
					$ancestors = array($post->ancestors);				
			} else {
				$ancestors = array($post->post_parent);
			}

			// Reverse the order so it's oldest to newest
			$ancestors = array_reverse($ancestors);

			// Add the current Page to the ancestors list (as we need it's title too)
			$ancestors[] = $post->ID;

			$links = array();			
			foreach ( $ancestors as $ancestor ) {
				$tmp  = array();
				$tmp['title'] 	= strip_tags( get_the_title( $ancestor ) );
				$tmp['url'] 	= get_permalink($ancestor);
				$tmp['cur'] = false;
				if ($ancestor == $post->ID) {
					$tmp['cur'] = true;
				}
				$links[] = $tmp;
			}

			$output = $homelink;
			foreach ( $links as $link ) {
				$output .= ' '.$args['sep'].' ';
				if (!$link['cur']) {
					$output .= '<a href="'.$link['url'].'">'.$link['title'].'</a>';
				} else {
					$output .= bold_or_not($link['title']);
				}
			}
		}
	}
	
	if ($args['labels']['prefix'] != "") {
		$output = $args['labels']['prefix']." ".$output;
	}
	
	if ($args['display']) {
		echo $args['prefix'] .  $output . $args['suffix'];
	} else {
		return $args['prefix'] . $output . $args['suffix'];
	}
}